package com.dftdla;

import com.dftdla.dao.pojo.ChatMsg;
import com.dftdla.result.ResponseResult;
import com.dftdla.server.ChatServer;
import com.dftdla.server.session.Session;
import com.dftdla.util.Code;
import com.dftdlaRedis.cache.RedisCache;
import com.rabbitmq.client.Command;
import io.netty.channel.Channel;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.SingleThreadEventLoop;
import io.netty.util.concurrent.AbstractEventExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.annotation.Resource;
import javax.annotation.security.RunAs;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 14501
 */
@SpringBootApplication
@EnableWebMvc
@Slf4j
@RestController
public class Application implements CommandLineRunner{

    @Resource
    private ChatServer chatServer;
    @Resource
    private Session session;

    static volatile boolean chatServerRunning = false;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    DefaultEventLoopGroup group = new DefaultEventLoopGroup(1);

    @RequestMapping("/init")
    public void run() {
        if(!chatServerRunning){
            chatServerRunning = true;
            log.info("启动Netty服务器");
            group.execute(()->chatServer.run());
        }else{
            log.info("Netty服务器正在运行");
        }
    }

    @RequestMapping("/allMsg")
    public String allMsg(){
        return "ok:" + session.listMsg(new ResponseResult<>(Code.SUCCESS.code,"你好").toString());
    }

    @Resource
    private RedisCache redisCache;

    @Override
    public void run(String... args) {
        run();
    }
}
